package com.wang.yygh.cmn.controller;

import com.wang.yygh.cmn.service.DictService;
import com.wang.yygh.common.result.Result;
import com.wang.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin //跨域 配置gateway网关跨域后注释
public class DictController {

    @Autowired
    private DictService dictService;

    //导入数据字典
    @PostMapping("importData")
    public Result importData(MultipartFile file) { //获取上传的文件
        dictService.importDictData(file);
        return Result.ok();
    }


    //导出数据的接口
    @GetMapping("exportData")
    public  void exportData(HttpServletResponse response){
        dictService.exportDictData(response);
    }


    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildrenData/{id}")
    public Result findChildrenData(@PathVariable Long id){
        List<Dict> list = dictService.findChildrenData(id);
        return Result.ok(list);
    }

    //根据dictcode 和 value进行查询
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value){
        String dictName = dictService.getDictName(dictCode,value);
        return  dictName;
    }
    //根据value查询
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value){
        String dictName = dictService.getDictName("",value);
        return  dictName;
    }

    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return  Result.ok(list);
    }
}
